package jnpf.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.base.UserInfo;
import jnpf.config.PermissionsConfig;
import jnpf.entity.ZXGL_RenWuBianGengEntity;
import jnpf.entity.ZXGL_XiangMuFenJie_mingxibiaoEntity;
import jnpf.mapper.ZXGL_RenWuBianGengMapper;
import jnpf.model.zxgl_renwubiangeng.ZXGL_RenWuBianGengPagination;
import jnpf.permission.service.AuthorizeService;
import jnpf.service.ZXGL_RenWuBianGengService;
import jnpf.service.ZXGL_XiangMuFenJie_mingxibiaoService;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * ZXGL_RenWuBianGeng
 * 版本： V2.0
 * 版权： 河北交投智能交通技术有限责任公司
 * 作者： 数字交投项目组
 * 日期： 2025-03-22
 */
@Slf4j
@Service
public class ZXGL_RenWuBianGengServiceImpl extends ServiceImpl<ZXGL_RenWuBianGengMapper, ZXGL_RenWuBianGengEntity> implements ZXGL_RenWuBianGengService {


    @Autowired
    private UserProvider userProvider;

    @Autowired
    private AuthorizeService authorizeService;
    @Resource
    PermissionsConfig permissionsConfig;

    @Autowired
    private ZXGL_XiangMuFenJie_mingxibiaoService zxglXiangMuFenJieMingxibiaoService;


    @Override
    public List<ZXGL_RenWuBianGengEntity> getList(ZXGL_RenWuBianGengPagination zXGL_RenWuBianGengPagination) {

        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_RenWuBianGengEntity> zXGL_RenWuBianGengQueryWrapper = new QueryWrapper<>();
        //TODO 增加权限逻辑
        UserInfo userInfo = userProvider.get();
        if (!userInfo.getIsAdministrator()) {
            String alldataRoleId = permissionsConfig.getAlldataRoleid();
            List<String> roleIds = userInfo.getRoleIds();
            if (roleIds == null || (StringUtils.isNotBlank(alldataRoleId) && !roleIds.contains(alldataRoleId))) {
                String userId = userInfo.getUserId();
                //任务变更：创建人看本人数据；主责部门负责人看本部门数据；主责部门主管领导看所管理部门数据；
                zXGL_RenWuBianGengQueryWrapper.lambda().and(wrapper -> wrapper
                        .eq(ZXGL_RenWuBianGengEntity::getCreatorUserId, userId)//创建人看本人数据
                        .or()
                        .eq(ZXGL_RenWuBianGengEntity::getDepartmentManagerZzbm, userId)//主责部门负责人看本部门数据
                        .or()
                        .eq(ZXGL_RenWuBianGengEntity::getDepartmentHeadleaderZzbm, userId) //主责部门主管领导看所管理部门数据
                );
            }
        }
        int zXGL_RenWuBianGengNum = 0;
        if (StringUtil.isNotEmpty(zXGL_RenWuBianGengPagination.getBglx())) {
            zXGL_RenWuBianGengNum++;
            zXGL_RenWuBianGengQueryWrapper.lambda().like(ZXGL_RenWuBianGengEntity::getBglx, zXGL_RenWuBianGengPagination.getBglx());
        }

        if (StringUtil.isNotEmpty(zXGL_RenWuBianGengPagination.getRwmc())) {
            zXGL_RenWuBianGengNum++;
            zXGL_RenWuBianGengQueryWrapper.lambda().like(ZXGL_RenWuBianGengEntity::getRwmc, zXGL_RenWuBianGengPagination.getRwmc());
        }

        if (StringUtil.isNotEmpty(zXGL_RenWuBianGengPagination.getXmrwlx())) {
            zXGL_RenWuBianGengNum++;
            zXGL_RenWuBianGengQueryWrapper.lambda().eq(ZXGL_RenWuBianGengEntity::getXmrwlx, zXGL_RenWuBianGengPagination.getXmrwlx());
        }
        if (StringUtil.isNotEmpty(zXGL_RenWuBianGengPagination.getXmid())) {
            zXGL_RenWuBianGengNum++;
            zXGL_RenWuBianGengQueryWrapper.lambda().eq(ZXGL_RenWuBianGengEntity::getXmid, zXGL_RenWuBianGengPagination.getXmid());
        }
        if (AllIdList.size() > 0) {
            zXGL_RenWuBianGengQueryWrapper.lambda().in(ZXGL_RenWuBianGengEntity::getId, AllIdList);
        }
        /**
         * 数据权限 此处手动写入菜单id
         */
//        String moduleId = "673071807217038981";
//        if (!userProvider.get().getIsAdministrator()) {
//            Object conditionObj = authorizeService.getCondition(zXGL_RenWuBianGengQueryWrapper, userProvider.get(), moduleId);
//            if (ObjectUtil.isEmpty(conditionObj)) {
//                return new ArrayList<>();
//            }
//            zXGL_RenWuBianGengQueryWrapper = (QueryWrapper<ZXGL_RenWuBianGengEntity>) authorizeService.getCondition(zXGL_RenWuBianGengQueryWrapper, userProvider.get(), moduleId);
//        }
        //排序
//        if (StringUtil.isEmpty(zXGL_RenWuBianGengPagination.getSidx())) {
//            zXGL_RenWuBianGengQueryWrapper.lambda().orderByDesc(ZXGL_RenWuBianGengEntity::getRwmc);
//        } else {
//            try {
//                String sidx = zXGL_RenWuBianGengPagination.getSidx();
//                ZXGL_RenWuBianGengEntity zXGL_RenWuBianGengEntity = new ZXGL_RenWuBianGengEntity();
//                Field declaredField = zXGL_RenWuBianGengEntity.getClass().getDeclaredField(sidx);
//                declaredField.setAccessible(true);
//                String value = declaredField.getAnnotation(TableField.class).value();
//                zXGL_RenWuBianGengQueryWrapper = "asc".equals(zXGL_RenWuBianGengPagination.getSort().toLowerCase()) ? zXGL_RenWuBianGengQueryWrapper.orderByAsc(value) : zXGL_RenWuBianGengQueryWrapper.orderByDesc(value);
//            } catch (NoSuchFieldException e) {
//                e.printStackTrace();
//            }
//        }
        zXGL_RenWuBianGengQueryWrapper.lambda().orderByDesc(ZXGL_RenWuBianGengEntity::getCreatorTime);
        if ((total > 0 && AllIdList.size() > 0) || total == 0) {
            Page<ZXGL_RenWuBianGengEntity> page = new Page<>(zXGL_RenWuBianGengPagination.getCurrentPage(), zXGL_RenWuBianGengPagination.getPageSize());
            IPage<ZXGL_RenWuBianGengEntity> userIPage = this.page(page, zXGL_RenWuBianGengQueryWrapper);
            return zXGL_RenWuBianGengPagination.setData(userIPage.getRecords(), userIPage.getTotal());
        } else {
            List<ZXGL_RenWuBianGengEntity> list = new ArrayList();
            return zXGL_RenWuBianGengPagination.setData(list, list.size());
        }
    }

    @Override
    public List<ZXGL_RenWuBianGengEntity> getTypeList(ZXGL_RenWuBianGengPagination zXGL_RenWuBianGengPagination, String dataType) {
        String userId = userProvider.get().getUserId();
        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_RenWuBianGengEntity> zXGL_RenWuBianGengQueryWrapper = new QueryWrapper<>();
        int zXGL_RenWuBianGengNum = 0;
        if (StringUtil.isNotEmpty(zXGL_RenWuBianGengPagination.getBglx())) {
            zXGL_RenWuBianGengNum++;
            zXGL_RenWuBianGengQueryWrapper.lambda().like(ZXGL_RenWuBianGengEntity::getBglx, zXGL_RenWuBianGengPagination.getBglx());
        }

        if (StringUtil.isNotEmpty(zXGL_RenWuBianGengPagination.getRwmc())) {
            zXGL_RenWuBianGengNum++;
            zXGL_RenWuBianGengQueryWrapper.lambda().like(ZXGL_RenWuBianGengEntity::getRwmc, zXGL_RenWuBianGengPagination.getRwmc());
        }

        if (StringUtil.isNotEmpty(zXGL_RenWuBianGengPagination.getXmrwlx())) {
            zXGL_RenWuBianGengNum++;
            zXGL_RenWuBianGengQueryWrapper.lambda().eq(ZXGL_RenWuBianGengEntity::getXmrwlx, zXGL_RenWuBianGengPagination.getXmrwlx());
        }

        /**
         * 数据权限 此处手动写入菜单id
         */
//        String moduleId = "";
//        if (!userProvider.get().getIsAdministrator()) {
//            Object conditionObj = authorizeService.getCondition(zXGL_RenWuBianGengQueryWrapper, userProvider.get(), moduleId);
//            if (ObjectUtil.isEmpty(conditionObj)) {
//                return new ArrayList<>();
//            }
//            zXGL_RenWuBianGengQueryWrapper = (QueryWrapper<ZXGL_RenWuBianGengEntity>) authorizeService.getCondition(zXGL_RenWuBianGengQueryWrapper, userProvider.get(), moduleId);
//        }
        if (AllIdList.size() > 0) {
            zXGL_RenWuBianGengQueryWrapper.lambda().in(ZXGL_RenWuBianGengEntity::getId, AllIdList);
        }
        //排序
        if (StringUtil.isEmpty(zXGL_RenWuBianGengPagination.getSidx())) {
            zXGL_RenWuBianGengQueryWrapper.lambda().orderByDesc(ZXGL_RenWuBianGengEntity::getRwmc);
        } else {
            try {
                String sidx = zXGL_RenWuBianGengPagination.getSidx();
                ZXGL_RenWuBianGengEntity zXGL_RenWuBianGengEntity = new ZXGL_RenWuBianGengEntity();
                Field declaredField = zXGL_RenWuBianGengEntity.getClass().getDeclaredField(sidx);
                declaredField.setAccessible(true);
                String value = declaredField.getAnnotation(TableField.class).value();
                zXGL_RenWuBianGengQueryWrapper = "asc".equals(zXGL_RenWuBianGengPagination.getSort().toLowerCase()) ? zXGL_RenWuBianGengQueryWrapper.orderByAsc(value) : zXGL_RenWuBianGengQueryWrapper.orderByDesc(value);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        if ("0".equals(dataType)) {
            if ((total > 0 && AllIdList.size() > 0) || total == 0) {
                Page<ZXGL_RenWuBianGengEntity> page = new Page<>(zXGL_RenWuBianGengPagination.getCurrentPage(), zXGL_RenWuBianGengPagination.getPageSize());
                IPage<ZXGL_RenWuBianGengEntity> userIPage = this.page(page, zXGL_RenWuBianGengQueryWrapper);
                return zXGL_RenWuBianGengPagination.setData(userIPage.getRecords(), userIPage.getTotal());
            } else {
                List<ZXGL_RenWuBianGengEntity> list = new ArrayList();
                return zXGL_RenWuBianGengPagination.setData(list, list.size());
            }
        } else {
            return this.list(zXGL_RenWuBianGengQueryWrapper);
        }
    }

    @Override
    public ZXGL_RenWuBianGengEntity getInfo(String id) {
        QueryWrapper<ZXGL_RenWuBianGengEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZXGL_RenWuBianGengEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void create(ZXGL_RenWuBianGengEntity entity) {
        this.save(entity);
    }

    @Override
    public boolean update(String id, ZXGL_RenWuBianGengEntity entity) {
        entity.setId(id);
        return this.updateById(entity);
    }

    @Override
    public Boolean updateFlow(String id) {
        ZXGL_RenWuBianGengEntity zxglXiangMuBianGengEntity = baseMapper.selectById(id);
        ZXGL_XiangMuFenJie_mingxibiaoEntity zxglXiangMuLiXiangMingXiEntity = zxglXiangMuFenJieMingxibiaoService.getInfo(zxglXiangMuBianGengEntity.getMainid());
        if (null != zxglXiangMuBianGengEntity) {
            //变更类型选的哪个更新那个
            if (null != zxglXiangMuBianGengEntity.getBglx()) {
                if (zxglXiangMuBianGengEntity.getBglx().indexOf("672046045261231749") != -1) {
                    zxglXiangMuLiXiangMingXiEntity.setRwmc(zxglXiangMuBianGengEntity.getBghrwmc());
                }
                if (zxglXiangMuBianGengEntity.getBglx().indexOf("672046075254699653") != -1) {
                    zxglXiangMuLiXiangMingXiEntity.setZt(zxglXiangMuBianGengEntity.getBghrwzt());
                }
                if (zxglXiangMuBianGengEntity.getBglx().indexOf("672046159518267013") != -1) {
                    zxglXiangMuLiXiangMingXiEntity.setJhksrq(zxglXiangMuBianGengEntity.getBghjhksrq());
                }
                if (zxglXiangMuBianGengEntity.getBglx().indexOf("672046191248176773") != -1) {
                    zxglXiangMuLiXiangMingXiEntity.setJhwcrq(zxglXiangMuBianGengEntity.getBghjhjsrq());
                }
                if (zxglXiangMuBianGengEntity.getBglx().indexOf("672046232260081285") != -1) {
                    zxglXiangMuLiXiangMingXiEntity.setGzmb(zxglXiangMuBianGengEntity.getBiangenghougongzuomubiao());
                }
                if (zxglXiangMuBianGengEntity.getBglx().indexOf("672051552483047045") != -1) {
                    zxglXiangMuLiXiangMingXiEntity.setSjz(zxglXiangMuBianGengEntity.getBghjhz());
                }
//                if(zxglXiangMuBianGengEntity.getBglx().equals("672051552483047045")){
//                    zxglXiangMuLiXiangMingXiEntity.setRwjd(zxglXiangMuBianGengEntity.getBghjhz());
//                }
            }
            return zxglXiangMuFenJieMingxibiaoService.updateById(zxglXiangMuLiXiangMingXiEntity);
        }
        return true;

    }

    @Override
    public void delete(ZXGL_RenWuBianGengEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }
    }
    //子表方法

    //列表子表数据方法

}
